package com.atguigu.demo.graph;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;

public class Img {
    public static void hello(){
        try {

            FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(new Font("宋体", Font.PLAIN, 22));//设计字体

            BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_BGR);//设置宽高，图片类型

            Graphics g = image.getGraphics();

            g.setClip(0, 0, 800, 600);//设置绘画的区域,默认背景是黑色，也叫做获取画笔，指定画笔可画的范围

            g.setColor(Color.white);//设置画笔颜色

            g.fillRect(0, 0, 800, 600);//画一个矩形块,颜色是白色，因为画笔颜色是白色

            g.setColor(Color.black);//设置画笔颜色

            g.setFont(new Font("宋体", Font.PLAIN, 22));//设计字体样式

            g.drawString("123456123456",20,50);//画字符串
            g.drawString("12456123456",20,100);

            g.dispose();
            String srcImgPath = "D:/qrCode/img/111/DD"+System.currentTimeMillis()+".png";//添加水印后保持路径
            OutputStream os   = new FileOutputStream(srcImgPath);

            ImageIO.write(image, "png",os);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void ImageTest() throws Exception {

        String ext="png";

        FileInputStream in = new FileInputStream("D:/qrCode/img/111/DD1660554708044.png");

        byte[] bytes = new byte[in.available()];

        in.read(bytes);

        in.close();

        Random random=new Random(System.currentTimeMillis());

        ImageIcon imageIcon = new ImageIcon(bytes);

        BufferedImage bufferedImage=new BufferedImage(imageIcon.getIconHeight(),imageIcon.getIconWidth(),BufferedImage.TYPE_INT_RGB);

        Graphics2D g=(Graphics2D)bufferedImage.getGraphics();

        g.setColor(Color.blue);

        g.drawRect(5,5,5,5);

        g.fillRect(5,5,5,5);

        g.drawImage(imageIcon.getImage(),0,0,imageIcon.getIconHeight(),imageIcon.getIconWidth(),imageIcon.getImageObserver());

       // String filepath = System.getProperty("java.io.tmpdir") + random.nextInt(99999) + "." + ext;
        String filepath = "D:/qrCode/img/111/DD"+System.currentTimeMillis()+"." + ext;//添加水印后保持路径
        try {

            ImageIO.write(bufferedImage, ext,  new File(filepath));

            System.out.println("文件已经生成，路经为" + filepath);

        }

        catch (IOException ex) {

            ex.printStackTrace();

        }

    }

    /**

     * @param args

     */


    public static void main(String[] args) throws Exception {
     //   hello();
        ImageTest();
    }
}
